<?php
namespace Amoysea\Controller;
use Think\Controller;
class OrderController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		C('db_config.DB_NAME',C('erp_dbname'));
		//数据库连接基础配置-end
    }
	public function del_orderdetail(){
		$id=trim(I('id'));
		$m=M('Pushordergoods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//批量删除
	public function delAll_storagegoods(){
		$ids=I('ids');
		$map['id']=array('in',$ids);
		$m=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$status=$m->where($map)->delete();
		if($status){
			$return['status']=1;
			$return['info']="删除成功";
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['info']="出错";
			$this->ajaxReturn($return);
		}
	}
	public function view_goods(){
		$id=I('id');
		$m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$m_info=$m->find($id);
		$this->assign('vo',$m_info);
		$this->display();
	}
	//批量删除
	public function delAll_order(){
		$ids=I('ids');
		$map['id']=array('in',$ids);
		$m=M('Pushorder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$status=$m->where($map)->delete();
		if($status){
			$return['status']=1;
			$return['info']="删除成功";
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['info']="出错";
			$this->ajaxReturn($return);
		}
	}
	//批量删除
	public function delAll(){
		$ids=I('ids');
		$map['id']=array('in',$ids);
		$m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$status=$m->where($map)->delete();
		if($status){
			$return['status']=1;
			$return['info']="删除成功";
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['info']="出错";
			$this->ajaxReturn($return);
		}
	}
	//库存列表
	public function storagegoods(){
		$m=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$goods_no=I('goods_no');
		$goods_name=I('goods_name');
		$goods_brand=I('goods_brand');
		$excel=I('excel');
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($goods_no)){
			$map['goods_no'] = array('like','%'.$goods_no.'%');
        }
		if(!empty($goods_name)){
			$map['goods_name'] = array('like','%'.$goods_name.'%');
        }
		if(!empty($goods_brand)){
			$map['goods_brand'] = array('like','%'.$goods_brand.'%');
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$list[$k]['goods_price']=$this->goods_price($v['goods_price']);
			if($excel){
				$list[$k]['goods_no']="'".$v['goods_no'];
				$list[$k]['net_weight']=sprintf("%.2f",$v['net_weight']);
				$list[$k]['gross_weight']=sprintf("%.2f",$v['gross_weight']);
			}
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		if($excel){
			$xlsName  = "商品列表";
			$xlsCell  = array(
				array('goods_name','商品名称'),
				array('goods_no','商品编码'),
				array('goods_brand','品牌'),
				array('goods_marque','型号'),
				array('goods_spec','规格'),
				array('goods_price','价格'),
				array('storage_amount','库存'),
				array('net_weight','净重(g)'),
				array('gross_weight','毛重(g)')
			);
			$this->exportExcel($xlsName,$xlsCell,$list);
			die;
		}
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	}

	//商品列表
	public function goods(){
		// dump(session());
        $m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		
		$bar_code=I('bar_code');
		
		$goods_name=I('goods_name');
		$goods_brand=I('goods_brand');
		$excel=I('excel');
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($bar_code)){
			$map['bar_code'] = array('like','%'.$bar_code.'%');
        }
		if(!empty($goods_name)){
			$map['goods_name'] = array('like','%'.$goods_name.'%');
        }
		if(!empty($goods_brand)){
			$map['goods_brand'] = array('like','%'.$goods_brand.'%');
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$list[$k]['price']=$this->goods_price($v['price']);
			if($excel){
				$list[$k]['bar_code']="'".$v['bar_code'];
			}
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		if($excel){
			$xlsName  = "商品列表";
			$xlsCell  = array(
				array('goods_name','商品名称'),
				array('bar_code','条形码'),
				array('goods_brand','品牌'),
				array('goods_marque','型号'),
				array('goods_spec','规格'),
				array('price','价格'),
				array('receive_amount','数量'),
				array('net_weight','净重'),
				array('gross_weight','毛重')
			);
			$this->exportExcel($xlsName,$xlsCell,$list);
			die;
		}
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
		//excel导出方法
	public function exportExcel($expTitle,$expCellName,$expTableData){
			$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
			$fileName = $xlsTitle.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
			$cellNum = count($expCellName);
			$dataNum = count($expTableData);
			//vendor("PHPExcel.PHPExcel");
		   
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.IOFactory.php");
		   
		   $date = date("Y_m_d",time());
			$fileName .= "_{$date}.xls";

			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			//$objProps = $objPHPExcel->getProperties();
			
			//$objPHPExcel = new PHPExcel();
			$cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
			
			$objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
		    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle);//设置标题
			$objPHPExcel->getActiveSheet(0)->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //居中
			
			for($i=0;$i<$cellNum;$i++){
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]); 
			} 
			  // Miscellaneous glyphs, UTF-8   
			for($i=0;$i<$dataNum;$i++){
			  for($j=0;$j<$cellNum;$j++){
				$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
			  }             
			}  
			
			header('pragma:public');
			header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
			header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
			$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
			$objWriter->save('php://output');
			exit;
	}
	//添加
	function add_goodss(){
			$trans=M('', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$trans->startTrans();   // 开启事务
			$m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$g=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$param['add_time']=time();
			$bar_code=$param['bar_code'];
			$param['goods_no']=$param['bar_code'];
			$goods_no=$bar_code;//商品编码=条形码
			$nums=$param['receive_amount'];
			$param['storage_amount']=$param['receive_amount'];
			$map['goods_no']=$goods_no;
			if(empty($param['goods_name'])){
				alert_json(0,'商品名称不能为空');
			}
			if(empty($param['bar_code'])){
				alert_json(0,'条形码不能为空');
			}
			if(empty($param['storage_amount'])){
				alert_json(0,'库存不能为空');
			}
			if (!empty($_FILES)){
				$config = array(
					'maxSize'    =>    3145728,    
					'savePath'   =>    '/Import/',    
					'saveName'   =>    array('uniqid',''),    
					'exts'       =>    array('jpg','jpeg','png','gif'),    
					'autoSub'    =>    true,    
					'subName'    =>    array('date','Ymd'),);
				$upload = new \Think\Upload($config);// 实例化上传类
				$info   =   $upload->upload();    
				if(!$info) {// 上传错误提示错误信息        
					$this->error($upload->getError());    
				}
				$extension=$info['goods_pic']['ext'];
				$file_name='./Uploads'.$info['goods_pic']['savepath'].$info['goods_pic']['savename'];
				
				// textgd($file_name);//加水印
				$param['goods_pic']=$file_name;
			}
			// dump($param);
			$res=$m->add($param);
			if($res){
				$g_info=$g->where($map)->find();//更新库存
				if(empty($g_info)){//没商品信息则新增商品信息
					$data_goods['bar_code']=$param['bar_code'];
					$data_goods['goods_no']=$param['goods_no'];
					$data_goods['goods_name']=$param['goods_name'];
					$data_goods['goods_brand']=$param['goods_brand'];
					$data_goods['goods_marque']=$param['goods_marque'];
					$data_goods['goods_spec']=$param['goods_spec'];
					$data_goods['goods_pic']=$param['goods_pic'];
					$data_goods['goods_price']=$param['price'];
					$data_goods['storage_amount']=$param['receive_amount'];//库存数量
					$data_goods['amount']=$param['receive_amount'];//总数量
					$data_goods['net_weight']=$param['net_weight'];
					$data_goods['gross_weight']=$param['gross_weight'];
					$res_goods=$g->add($data_goods);
					if(!$res_goods){
						$trans->rollback();
						alert_json(0,'添加库存信息失败');
					}
				}else{//有商品信息则更新库存
					$this->edit_storagegoodsnums($goods_no,$nums);
				}
				$trans->commit();
				alert_json(1,'添加成功');
			}else{
				$trans->rollback();
				alert_json(0,'添加失败');
			}
	}
	//更新
	function add_goods(){
		if(IS_POST){
			$m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$param['update_time'] = time();
			$param['goods_no']=$param['bar_code'];
			if(empty($param['goods_name'])){
				alert_json(0,'商品名称不能为空');
			}
			if(empty($param['bar_code'])){
				alert_json(0,'条形码不能为空');
			}
			if(empty($param['receive_amount'])){
				alert_json(0,'库存不能为空');
			}
			$id=$param['id'];
			if (!empty($_FILES)){
				$config = array(
					'maxSize'    =>    3145728,    
					'savePath'   =>    '/Import/',    
					'saveName'   =>    array('uniqid',''),    
					'exts'       =>    array('jpg','jpeg','png','gif'),    
					'autoSub'    =>    true,    
					'subName'    =>    array('date','Ymd'),);
				$upload = new \Think\Upload($config);// 实例化上传类
				$info   =   $upload->upload();    
				if(!$info) {// 上传错误提示错误信息        
					$this->error($upload->getError());    
				}
				$extension=$info['goods_pic']['ext'];
				$file_name='./Uploads'.$info['goods_pic']['savepath'].$info['goods_pic']['savename'];
		
				// textgd($file_name);//加水印
				$param['goods_pic']=$file_name;
			}
			$param['storage_amount']=$param['receive_amount'];
			$res=$m->where(array('id'=>$id))->save($param);
			if($res!==false){
				alert_json(1,'更新成功');
			}
		}else{
			$this->display();
		}
	}
	//编辑
	function edit_goods(){
		$id=trim(I('id'));
		$m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_storagegoods(){
		$id=trim(I('id'));
		$m=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//删除
	function del_goods(){
		$id=trim(I('id'));
		$m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
				// $pushapi=R('push_yungoods',array('id'=>$res,'type'=>'delete'));
				// $djson=json_decode($pushapi);
				// if($djson['status']){
					// $return['message']='添加成功';
					// $return['status']=true;
					// $this->ajaxReturn($return);
				// }
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//推送oms
	public function pushoms(){
		$m=M('Pushorder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$g=M('Pushordergoods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$param=I('param.','','trim');
		$ids=$param['ids'];
		$url=C('base_config.pushoms');
		$map['id']=array('in',$ids);
		$list=$m->field('id,order_no,user_name,user_phone,user_idcard buyer_idcard,user_name buyer_name,province user_province,city user_city,area user_area,user_address,total_price order_amount')->where($map)->select();
		foreach($list as $k=>$v){
			$list[$k]['order_detail']=$g->field('goods_no goods_sn,goods_name,goods_nums,goods_price')->where(array('order_id'=>$v['id']))->select();
		}
		$data['data']=$list;
		$data['customer']['user_code']=session('company.user_code');
		$data['customer']['user_key']=session('company.user_key');
		// dump($data);
		$res=send_curl($url,$data);
		$res=json_decode($res,true);
		if($res['flag']){
			//更新运单号
			foreach($res['data']['detail'] as $ks=>$vs){
				$data_order['logistics_no']=$vs['logistics_no'];
				$m->where(array('order_no'=>$vs['order_no']))->save($data_order);
			}
			$return['message']=$res['tip'];
			$return['status']=true;
			$this->ajaxReturn($return);
		}else{
			$return['message']=$res['tip'];
			$return['status']=false;
			$this->ajaxReturn($return);
		}
	}
	//订单列表
	public function order(){
        $m=M('Pushorder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$n=M('Pushordergoods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$model=M('', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));

		$param = I('param.','','trim');
		$order_no=$param['order_no'];
		$logistics_no=$param['logistics_no'];
		$user_name=$param['user_name'];
		$user_idcard=$param['user_idcard'];
		$user_phone=$param['user_phone'];
		$user_address=$param['user_address'];
		$start_time=$param['start_time'];
		$end_time=$param['end_time'];
		$flag=$param['flag'];
		
		$order_cate2=$param['order_cate2'];
		
		$goods_name=$param['goods_name'];
		$sendMan=$param['sendMan'];
		$pay_status=$param['pay_status'];
		
		!empty($start_time)?$start_time = strtotime($start_time):$start_time=0;
		!empty($end_time)?$end_time = strtotime($end_time):$end_time =time();
		$map['_string'] = "add_time>=$start_time AND add_time<=$end_time";
		if(!empty($flag)){
			$map['flag']=$flag;
		}
		if(!empty($order_cate2)){
			$map['order_cate2']=$order_cate2;
		}
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		
		if(!empty($pay_status)){
			$map['o.pay_status'] = $pay_status;
        }
		
        if(!empty($order_no)){
			$map['o.order_no'] = array('like','%'.$order_no.'%');
        }
		if(!empty($logistics_no)){
			$map['o.logistics_no'] = array('like','%'.$logistics_no.'%');
        }
		if(!empty($user_idcard)){
			$map['o.user_idcard'] = array('like','%'.$user_idcard.'%');
        }
		if(!empty($user_phone)){
			$map['o.user_phone'] = array('like','%'.$user_phone.'%');
        }
		if(!empty($user_address)){
			$map['o.user_address'] = array('like','%'.$user_address.'%');
        }
		if(!empty($user_name)){
			$map['o.user_name'] = array('like','%'.$user_name.'%');
        }
		if(!empty($sendMan)){
			$map['o.sendMan'] = array('like','%'.$sendMan.'%');
        }
		$excel=I('excel');
		if(!empty($goods_name)){
			$map['g.goods_name'] = array('like','%'.$goods_name.'%');
			//生成子查询sql语句
			$sql=$n->alias('g')->join('__PUSHORDER__ o ON g.order_id= o.id','LEFT')->field('g.order_id,g.goods_nums,g.goods_name,o.province,o.flag,o.city,o.area,o.id,o.order_no,o.logistics_no,o.times,o.add_time,o.user_idcard,o.user_name,o.user_phone,o.user_address')->where($map)->buildSql();
			// echo $sql;
			if(!empty($excel)){ //导出Excel
				$list_count=$model->table($sql.' a')->select();
				$count=count($list_count);
				$list=$model->table($sql.' a')->limit($firstrow.','.$listrows)->order('id desc')->select();
			}else{
				$list_count=$model->table($sql.' a')->group('order_id')->select();
				$count=count($list_count);
				$list=$model->table($sql.' a')->group('order_id')->limit($firstrow.','.$listrows)->order('id desc')->select();
			}
			
			// echo $model->getlastsql();
			// dump($list);
		}else{
			if(!empty($excel)){ //导出Excel
				$count=$n->alias('g')->join('__PUSHORDER__ o ON g.order_id= o.id','LEFT')->field('g.order_id,g.goods_no,g.goods_price,g.goods_nums,g.goods_name,o.sendMan,o.sendManPhone,o.sendManAddress,o.sendProvince,o.sendCity,o.sendCounty,o.pay_status,o.order_cate2,o.province,o.flag,o.city,o.area,o.id,o.order_no,o.logistics_no,o.times,o.add_time,o.user_idcard,o.user_name,o.user_phone,o.user_address')->where($map)->count();
				$list=$n->alias('g')->join('__PUSHORDER__ o ON g.order_id= o.id','LEFT')->field('g.order_id,g.goods_no,g.goods_price,g.goods_nums,g.goods_name,o.sendMan,o.sendManPhone,o.sendManAddress,o.sendProvince,o.sendCity,o.sendCounty,o.pay_status,o.order_cate2,o.province,o.flag,o.city,o.area,o.id,o.order_no,o.logistics_no,o.times,o.add_time,o.user_idcard,o.user_name,o.user_phone,o.user_address')->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			}else{
				$count=$m->alias('o')->where($map)->count();
				$list=$m->alias('o')->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			}
		}
		// echo $n->getlastsql();
		// dump($list);
		// die;
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		// echo $g->getlastsql();
		// dump($list);
		//导出Excel
		// $excel=I('excel');
		$region=M('Region');
		foreach($list as $k=>$v){
			$list[$k]['province']=$region->where(array('code'=>$v['province']))->getField('fullName');
			$list[$k]['city']=$region->where(array('code'=>$v['city']))->getField('fullName');
			$list[$k]['area']=$region->where(array('code'=>$v['area']))->getField('fullName');
			$list[$k]['goods_list']=$n->where(array('order_id'=>$v['id']))->select();
			if($excel){
				$list[$k]['goods_no']="'".$v['goods_no'];
				$list[$k]['sendManPhone']="'".$v['sendManPhone'];
				$list[$k]['user_phone']="'".$v['user_phone'];
				$list[$k]['user_idcard']="'".$v['user_idcard'];
			}
		}
			
		if(!empty($excel)){ //导出Excel
			$xlsName  = "订单管理";
			$xlsCell  = array(
				array('order_no','订单编号'),
				array('logistics_no','运单号'),
				array('user_name','收件人姓名'),
				array('user_idcard','身份证号'),
				array('user_phone','收件人电话'),
				array('province','收件人省'),
				array('city','收件人市'),
				array('area','收件人区'),
				array('user_address','收件人详细地址'),
				array('total_price','总价'),
				array('sendMan','发件人姓名'),
				array('sendManPhone','发件人电话'),
				array('sendManAddress','发件人详细地址'),
				array('sendProvince','发件人省'),
				array('sendCity','发件人市'),
				array('sendCounty','发件人区/县'),
				array('goods_no','商品编号'),
				array('goods_name','商品名称'),
				array('goods_nums','商品数量'),
				array('goods_price','商品单价'),
				array('pay_status','付费状态（0:未设置1:已付费2:未付费）'),
				array('flag','状态(1:绿2:黄3:红)'),
				array('order_cate2','订单类型(0:未设置1:现货2:直邮3:保税)')
			);
			$this->exportExcel($xlsName,$xlsCell,$list);
			die;
		}
		
		
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//订单类型
	public function defaults1(){
		$val=I('val');
		$status=I('status');
		if($status=='true'){
			$success="设置成功";
			$failed="设置失败";
		}else{
			$success="取消成功";
			$failed="取消失败";
		}
		$old=file_get_contents('txt/defaultsq.txt');
		$arr=json_decode($old,true);
		$arr[0]['cate']='order_cate2';
		$arr[0]['val']=$val;
		$arr[0]['status']=$status;
		$text=json_encode($arr);
		$res=file_put_contents('txt/defaultsq.txt',$text);
		if($res){
			alert_json(1,$success);
		}else{
			alert_json(0,$failed);
		}
	}
	//收件人
	public function defaults2(){
		$val=I('val');
		$status=I('status');
		if($status=='true'){
			$success="设置成功";
			$failed="设置失败";
		}else{
			$success="取消成功";
			$failed="取消失败";
		}
		$old=file_get_contents('txt/defaultsq.txt');
		$arr=json_decode($old,true);
		$arr[1]['cate']='user_name';
		$arr[1]['val']=$val;
		$arr[1]['status']=$status;
		$text=json_encode($arr);
		$res=file_put_contents('txt/defaultsq.txt',$text);
		if($res){
			alert_json(1,$success);
		}else{
			alert_json(0,$failed);
		}
	}
	//发件人
	public function defaults3(){
		$val=I('val');
		$status=I('status');
		if($status=='true'){
			$success="设置成功";
			$failed="设置失败";
		}else{
			$success="取消成功";
			$failed="取消失败";
		}
		$old=file_get_contents('txt/defaultsq.txt');
		$arr=json_decode($old,true);
		$arr[2]['cate']='sendMan';
		$arr[2]['val']=$val;
		$arr[2]['status']=$status;
		$text=json_encode($arr);
		$res=file_put_contents('txt/defaultsq.txt',$text);
		if($res){
			alert_json(1,$success);
		}else{
			alert_json(0,$failed);
		}
	}
	//付款状态
	public function defaults4(){
		$val=I('val');
		$status=I('status');
		if($status=='true'){
			$success="设置成功";
			$failed="设置失败";
		}else{
			$success="取消成功";
			$failed="取消失败";
		}
		$old=file_get_contents('txt/defaultsq.txt');
		$arr=json_decode($old,true);
		$arr[3]['cate']='pay_status';
		$arr[3]['val']=$val;
		$arr[3]['status']=$status;
		$text=json_encode($arr);
		$res=file_put_contents('txt/defaultsq.txt',$text);
		if($res){
			alert_json(1,$success);
		}else{
			alert_json(0,$failed);
		}
	}
	public function order_detail(){
		$g=M('Pushordergoods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$p=M('Pushorder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		
		$param=I('param.','','trim');
		$id=$param['id'];
		$p_info=$p->find($id);
		$this->assign('detail',$p_info);
		
		$count=$g->where(array('order_id'=>$id))->count();
		$list=$g->alias('g')
				->field('g.id,g.goods_no,g.goods_name,g.goods_nums,g.goods_price,ao.goods_brand,ao.goods_marque,ao.goods_spec,ao.goods_pic,ao.net_weight,ao.gross_weight')
				->join('__GOODS__ ao on g.goods_no=ao.goods_no','left')
				->where(array('g.order_id'=>$id))
				->select();
		foreach($list as $k=>$v){
			$list[$k]['goods_price']=$this->goods_price($v['goods_price']);
		}
		$this->assign('count',$count);
		$this->assign('list',$list);
		$this->display();
	}
	public function edit_orderdetail(){
		$trans=M('', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$trans->startTrans();   // 开启事务
		$m=M('Pushordergoods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$p=M('Pushorder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		// dump(I());
		$id=I('id');
		$data['goods_nums']=I('count');
		$info=$m->where(array('id'=>$id))->find();
		if(empty($info)){
			$trans->rollback();
			alert_json(0,'没有找到数据');
		}
		$res=$m->where(array('id'=>$id))->save($data);
		$p_info=$p->where(array('id'=>$info['order_id']))->find();
		if(empty($p_info)){
			$trans->rollback();
			alert_json(0,'没有找到订单');
		}
		if($res!==false){
			//扣库存
			$res_dd=$this->edit_storagegoodsnums($info['goods_no'],$data['goods_nums'],false);
			// dump($res_dd);
			//扣订单金额
			$childs=session('company.childs');
			// dump($childs);
			if(!empty($childs)){
				$username=$childs.username;
				$res_outfee=$this->outfee($username,$info['goods_price']*$info['goods_nums']);
				if(!$res_outfee){
					alert_json(1,'扣费失败');
				}
			}
			//添加对账单
			$checkbill=A('Checkbill');
			$res_checkbill=$checkbill->confirm_adds($info['goods_price']*$info['goods_nums'],$p_info['order_no']);
			if($res_checkbill){
				$trans->commit();
				alert_json(1,'修改成功');
			}else{
				$trans->rollback();
				alert_json(1,'修改失败');
			}
		}else{
			$trans->rollback();
			alert_json(0,'修改失败');
		}
	}
	public function edit_storagegoods(){
		$m=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$id=I('id');
		$data['storage_amount']=I('count');
		
		$res=$m->where(array('id'=>$id))->save($data);
		if($res!==false){
			$this->success('修改成功');
		}else{
			$this->error('修改失败');
		}
		
	}
	//订单详细添加
	public function add_orderdetail(){
        $m=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$goods_no=I('goods_no');
		$goods_name=I('goods_name');
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($goods_no)){
			$map['goods_no'] = array('like','%'.$goods_no.'%');
        }
		if(!empty($goods_name)){
			$map['goods_name'] = array('like','%'.$goods_name.'%');
        }
		//去除已添加商品-begin
		$g=M('Pushordergoods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$order_id=I('order_id');
		$g_list=$g->field('goods_no')->where(array('order_id'=>$order_id))->select();
		foreach($g_list as $k=>$v){
			$goods_no_arr[$k]=$v['goods_no'];
		}
		if(!empty($g_list)){
			$where['goods_no']=array('not in',$goods_no_arr);
		}
		//去除已添加商品-end
		$count=$m->where($map)->where($where)->count();
		$list=$m->where($map)->where($where)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	}
	//添加商品
	public function tadd_goods(){
		$g=M('Pushordergoods', C('DB_PREFIX_T'), C('db_config'));
		$p=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
		$ao=M('Goods', C('DB_PREFIX_T'), C('db_config'));
		$trans = M('',C('DB_PREFIX_T'), C('db_config'));
        $trans->startTrans();   // 开启事务
		$param = I('param.','','trim');
		$data=explode(',',$param['str']);
		
		$order_no=$p->where(array('id'=>$param['order_id']))->getField('order_no');
		foreach($data as $k=>$v){
			$str_s=explode(':',$v);
			$data_s[$k]['order_id']=$param['order_id'];
			$data_s[$k]['goods_nums']=$str_s[0];
			$data_s[$k]['goods_price']=$str_s[1];
			$data_s[$k]['goods_name']=$str_s[3];
			$data_s[$k]['spec']=$ao->where(array('id'=>$str_s[4]))->getField('goods_spec');
			$data_s[$k]['goods_no']=$str_s[2];//商品编码=条形码
			
			//扣库存
			$res_edit=$this->edit_storagegoodsnums($data_s[$k]['goods_no'],$data_s[$k]['goods_nums'],false);
			if(!$res_edit){
				$trans->rollback();
			}
			//扣订单金额
			$childs=session('company.childs');
			if(!empty($childs)){
				$username=$childs['username'];
				$res_outfee=$this->outfee($username,$data_s[$k]['goods_price']*$data_s[$k]['goods_nums']);
				if(!$res_outfee){
					$trans->rollback();
				}
			}
			//添加对账单
			$checkbill=A('Checkbill');
			$res_checkbill=$checkbill->confirm_adds($data_s[$k]['goods_price']*$data_s[$k]['goods_nums'],$order_no);
			if(!$res_checkbill){
				$trans->rollback();
			}
		}
		$res=$g->addAll($data_s);//添加订单详细信息
		if($res){
			$trans->commit();
			$return['message']='添加商品成功';
			$return['status']=true;
			$this->ajaxReturn($return);
		}else{
			$trans->rollback();
			$return['message']='添加商品失败';
			$return['status']=false;
			$this->ajaxReturn($return);
		}
	}
	//编辑商品
	public function tedit_goods(){
		$g=M('Pushordergoods', C('DB_PREFIX_T'), C('db_config'));
		$ao=M('Goods', C('DB_PREFIX_T'), C('db_config'));
		$param = I('param.','','trim');
		$data=explode(',',$param['str']);
		foreach($data as $k=>$v){
			$str_s=explode(':',$v);
			$data_s[$k]['order_id']=$param['order_id'];
			$data_s[$k]['goods_nums']=$str_s[0];
			$data_s[$k]['goods_price']=$str_s[1];
			$data_s[$k]['goods_name']=$str_s[3];
			$data_s[$k]['spec']=$ao->where(array('id'=>$str_s[4]))->getField('goods_spec');
			$data_s[$k]['goods_no']=$str_s[2];//商品编码=条形码
		}
		$g->where(array('order_id'=>$param['order_id']))->delete();//清空
		$res=$g->addAll($data_s);//添加订单详细信息
		if($res){
			$return['message']='添加商品成功';
			$return['status']=true;
			$this->ajaxReturn($return);
		}else{
			$return['message']='添加商品失败';
			$return['status']=false;
			$this->ajaxReturn($return);
		}
	}
	public function adds_orderdetail(){
		$g=M('Pushordergoods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$ao=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$param = I('param.','','trim');
		$ids=$param['ids'];
		$order_id=$param['order_id'];
		$map['id']=array('in',$ids);
		$ao_list=$ao->where($map)->select();
		foreach($ao_list as $k=>$v){
			$data[$k]['order_id']=$order_id;
			$data[$k]['goods_no']=$v['goods_no'];
			$data[$k]['goods_name']=$v['goods_name'];
			$data[$k]['goods_price']=$v['goods_price'];
			$data[$k]['spec']=$v['goods_spec'];
			// $data[$k]['source']=$v['source'];
			// $data[$k]['bar_code']=$v['bar_code'];
		}
		// dump($data);
		$res=$g->addAll($data);//添加订单详细信息
		if($res){
			$return['message']='添加成功';
			$return['status']=true;
			$this->ajaxReturn($return);
		}else{
			$return['message']='添加失败';
			$return['status']=false;
			$this->ajaxReturn($return);
		}
	}
	//添加
	public function add_orders(){
			$m=M('Pushorder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$p=M('Printorder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$this->verification($param);
			$param['add_time']=time();
			
			$map['order_no']=I('order_no');
			$duplicate=$m->where($map)->find();
			if($duplicate){
				$return['message']='订单编码已经存在';
				$return['status']=0;
				$this->ajaxReturn($return);
			}
			
			$print_data['sendMan']=$param['sendMan'];
			$print_data['sendManPhone']=$param['sendManPhone'];
			$print_data['sendProvince']=$param['sendProvince'];
			$print_data['sendCity']=$param['sendCity'];
			$print_data['sendCounty']=$param['sendCounty'];
			$print_data['sendManAddress']=$param['sendManAddress'];
			$print_data['receiveMan']=$param['user_name'];
			$print_data['receiveManPhone']=$param['user_phone'];
			$print_data['receiveManAddress']=$param['user_address'];
			$print_data['receiveProvince']=$param['province'];
			$print_data['receiveCity']=$param['city'];
			$print_data['receiveCounty']=$param['area'];
			
			$res=$m->add($param);//添加订单信息
			
			if($res){
				$status=$p->add($print_data);//添加面单信息
				if(!$status){
					$m->where(array('id'=>$res))->delete();//回滚
					$return['message']='面单信息添加失败';
					$return['status']=0;
					$this->ajaxReturn($return);
				}
				$return['message']='添加成功';
				$return['order_id']=$res;
				$return['status']=1;
				$this->ajaxReturn($return);
			}
	}
	//验证
	public function verification($param){
			if(empty($param['order_no'])){
				$return['message']='订单编号不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			// if(empty($param['logistics_no'])){
				// $return['message']='电子面单号不能为空';
				// $return['status']=false;
				// $this->ajaxReturn($return);
			// }
			if(!mobile($param['user_phone'])){
				$return['message']='收件人电话(手机号)错误';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['user_name'])){
				$return['message']='收件人姓名不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['user_idcard'])){
				$return['message']='身份证号不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['user_phone'])){
				$return['message']='收件人电话不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['province'])){
				$return['message']='到件省不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['city'])){
				$return['message']='到件市不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['area'])){
				$return['message']='到件区/县不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['user_address'])){
				$return['message']='详细地址不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['total_price'])){
				$return['message']='总价不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(strlen($param['user_idcard'])!=15 and strlen($param['user_idcard'])!=18){
				$return['message']='身份证号位数必须为15位或18位';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
	}
	//身份证信息
	public function idcard_info(){
		$id=I('idcard_id');
		$idcard=M('Idcard', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$region=M('Region');
		$info=$idcard->where(array('id'=>$id))->find();
		$info['city_name']=$info['city'];
		$info['area_name']=$info['area'];
		if($info){
			alert_json(1,$info);
		}else{
			alert_json(0,'没有找到数据');
		}
	}
	public function city_list(){
		$code=I('code');
		$city_list=city_list($code);
		alert_json(1,$city_list);
	}
	public function area_list(){
		$code=I('code');
		$province_name=I('province_name');
		$area_list=area_list($province_name,$code);
		alert_json(1,$area_list);
	}
	//更新
	public function add_order(){
		$idcard=M('Idcard', C('DB_PREFIX_T'), C('db_config'));
		$g=M('Goods', C('DB_PREFIX_T'), C('db_config'));
		$e=M('EmsCompany', C('DB_PREFIX_T'), C('db_config'));
		$goods_list=$g->select();
		// dump($goods_list);
		// die;
		$ec_list=$e->where(array('status'=>1))->select();
		$this->assign('goods_list',$goods_list);
		$this->assign('ec_list',$ec_list);
		
		$old=file_get_contents('txt/defaultsq.txt');
		$arr=json_decode($old,true);
		foreach($arr as $k=>$v){
			if($v['status']=='true'){
				if($v['cate']=='order_cate2'){
					$detail['order_cate2']=$v['val'];
				}
				if($v['cate']=='user_name'){
					$idcard_info=$idcard->find($v['val']);
					$detail['user_name']=$idcard_info;
					
					$city_list=city_list($detail['user_name']['province']);
					$this->assign('city_list',$city_list);
					$area_list=area_list($detail['user_name']['province'],$detail['user_name']['city']);
					$this->assign('area_list',$area_list);
				}
				if($v['cate']=='sendMan'){
					$idcard_info=$idcard->find($v['val']);
					$detail['sendMan']=$idcard_info;
					
					$send_city_list=city_list($detail['sendMan']['province']);
					$this->assign('send_city_list',$send_city_list);
					$send_area_list=area_list($detail['sendMan']['province'],$detail['sendMan']['city']);
					$this->assign('send_area_list',$send_area_list);
				}
				if($v['cate']=='pay_status'){
					$detail['pay_status']=$v['val'];
				}
			}
		}

		$this->assign('detail',$detail);
		//流水号
		$outbound=A('Outbound');
		$serialNumber=$outbound->serialNumber('Pushorder');
		$serialNumber='P'.date("Ymd",time()).$serialNumber;
		$this->assign("serialNumber",$serialNumber);
		//流水号
		$userlist=$idcard->select();
		$this->assign('userlist',$userlist);
		$province_list=province_list();
		$this->assign('province_list',$province_list);
		if(IS_POST){
			$m=M('Pushorder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$p=M('Printorder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			// dump($param);
			$this->verification($param);
			
			$print_data['sendMan']=$param['sendMan'];
			$print_data['sendManPhone']=$param['sendManPhone'];
			$print_data['sendProvince']=$param['sendProvince'];
			$print_data['sendCity']=$param['sendCity'];
			$print_data['sendCounty']=$param['sendCounty'];
			$print_data['sendManAddress']=$param['sendManAddress'];
			$print_data['receiveMan']=$param['user_name'];
			$print_data['receiveManPhone']=$param['user_phone'];
			$print_data['receiveManAddress']=$param['user_address'];
			$print_data['receiveProvince']=$param['province'];
			$print_data['receiveCity']=$param['city'];
			$print_data['receiveCounty']=$param['area'];
			
			$param['update_time'] = time();
			$id=$param['id'];
			$res=$m->where(array('id'=>$id))->save($param);
			if($res!==false){
				$status=$p->where(array('order_no'=>$param['order_no']))->save($print_data);
				if($status===false){
					$return['message']='更新面单信息失败';
					$return['status']=false;
					$this->ajaxReturn($return);
				}
				$return['message']='更新成功';
				$return['status']=true;
				$this->ajaxReturn($return);
			}
		}else{
			$this->display();
		}
	}
	//编辑
	public function edit_order(){
		$e=M('EmsCompany', C('DB_PREFIX_T'), C('db_config'));
		$g=M('Goods', C('DB_PREFIX_T'), C('db_config'));
		$goods_list=$g->select();
		// dump($goods_list);
		// die;
		$ec_list=$e->where(array('status'=>1))->select();
		$this->assign('goods_list',$goods_list);
		$this->assign('ec_list',$ec_list);
		
		$idcard=M('Idcard', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$userlist=$idcard->select();
		$this->assign('userlist',$userlist);
		// dump(I());
		$id=trim(I('id'));
		$m=M('Pushorder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$g=M('Pushordergoods', C('DB_PREFIX_T'), C('db_config'));
		$detail=$m->where(array('id'=>$id))->find();
		$pgoods_list=$g->where(array('order_id'=>$id))->select();
		$this->assign('pgoods_list',$pgoods_list);
		
		$province_list=province_list();
		$this->assign('province_list',$province_list);
		$city_list=city_list($detail['province']);
		$this->assign('city_list',$city_list);
		$area_list=area_list($detail['province'],$detail['city']);
		$this->assign('area_list',$area_list);
		
		$send_city_list=city_list($detail['sendprovince']);
		$this->assign('send_city_list',$send_city_list);
		$send_area_list=area_list($detail['sendprovince'],$detail['sendcity']);
		$this->assign('send_area_list',$send_area_list);
		
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	public function del_order(){
		$id=trim(I('id'));
		$m=M('Pushorder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();
		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//更新总库存
	//$goods_no 商品编码
	//$nums 数量
	//$add 增加 false减少
	public function edit_storagegoodsnums($goods_no,$nums,$add=true,$inbound=false){
		$g=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['goods_no']=$goods_no;
		$info=$g->where($map)->find();
		// dump($info);
		if(empty($info)){
			alert_json(0,'库存中没有查到对应商品');
		}
		
		if($add){
			if($inbound){//入库则总数量叠加
				$data['amount']=array('exp','amount+'.$nums);//总数量
			}
			$data['storage_amount']=array('exp','storage_amount+'.$nums);//剩余库存
		}else{
			if($info['storage_amount']-$nums<0){
				alert_json(0,'库存不足');
			}
			$data['storage_amount']=array('exp','storage_amount-'.$nums);//剩余库存
		}
		$res=$g->where($map)->save($data);
		if($res!==false){
			return 1;
		}else{
			// $trans->rollback();
			alert_json(0,'扣减库存出错');
		}
	}
	
	//扣订单金额
	public function outfee($role_username,$outfee){
		$r=M('Role', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$data['remain_fee']=array('exp','remain_fee-'.$outfee);
		$r_info=$r->field('remain_fee')->where(array('username'=>$role_username))->find();
		if($r_info['remain_fee']-$outfee<0){
			alert_json(0,'余额不足');
		}
		$res=$r->where(array('username'=>$role_username))->save($data);
		
		if($res!==false){
			return 1;
		}else{
			// $trans->rollback();
			alert_json(0,'扣费出错');
		}
	}
	//商品价格
	public function goods_price($goods_price){
		$childs=session('company.childs');
		if(!empty($childs)){
			$price_level=$childs['price_level'];
			$price=$goods_price*$price_level/100;
			return $price;
		}else{
			return $goods_price;
		}
		
	}
	
}